import 'package:flutter/material.dart';

class Plan extends StatefulWidget {
  @override
  _PlanState createState() => _PlanState();
}

class _PlanState extends State<Plan> {
  List<Widget> widgets = List();
  List<String> items;
  List<int> list = [];
  ScrollController scrollController = ScrollController();

  List<String> data = List();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    for (int i = 0; i < 10; i++) {
      list.add(i);
    }
    items = List.generate(20, (int i) => '$i');
    for (String item in items) {
      widgets.add(Container(
        key: ValueKey(item),
        height: 100,
        margin: EdgeInsets.symmetric(horizontal: 50, vertical: 10),
        decoration: BoxDecoration(
            color: Colors.primaries[int.parse(item) % Colors.primaries.length],
            borderRadius: BorderRadius.circular(10)),
      ));
    }
    for (int i = 0; i < 10; i++) {
      data.add(i.toString());
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: 300,
//        height: 2500,
        child: listView(),
      ),
    );
  }

  initData() {}


  Widget listView(){

    return


      CustomScrollView(

      slivers: <Widget>[

        SliverToBoxAdapter(
          child: buildExpand(),

        ),

        SliverList(
            delegate:
            SliverChildBuilderDelegate(
                    (BuildContext context,int index){
                  return Text("11");
                },
                childCount: 10
            )
        )


      ],

    );

  }

  Widget buildExpand(){

    return ExpansionTile(title: Text("hehe"),children: <Widget>[
      level3List()

    ],);

  }
  Widget level1List() {
    return NotificationListener(
        child: ListView.builder(
          shrinkWrap: true,
      itemBuilder: (con, index) {
        return level2List(index);
      },
      itemCount: data.length,
          controller: scrollController,
    ),
    onNotification: (ScrollNotification noti){

          double sheight = noti.metrics.pixels;
          if(sheight>500){

            scrollController.jumpTo(sheight-500);
          }
          print("${sheight}");

    },
    );
  }

  Widget level2List(int index) {
//    return ExpansionTile(title: Text("hehe"),children: <Widget>[level3List()],);

    return Column(
      children: <Widget>[
        GestureDetector(
          child: buildItem(data[index].toString()),
          onTap: () {
            data.insert(index + 1, "呵呵哒");
            setState(() {});
          },
        ),
      ],
    );
    Container(
      child: level3List(),
    );
  }

  Widget buildItem(String s) {
    if (s == "呵呵哒") {
      return level3List();
    } else {
      return Text(s);
    }
  }

  Widget level3List() {
    return Container(
//        height: 1500,
      height: 1000,
      child: ReorderableListView(
        children: list.map(f).toList(),
        header: Text('This is the header!'),
        onReorder: (oldIndex, newIndex) {
          setState(() {
            _updateMyItems(oldIndex, newIndex);
          });
        },
      ),
    );
  }

//  Widget level4List() {
//    return Container(
////        height: 1500,
//      height: 1500,
//      child: ReorderableListView(
//        children: list.map(f).toList(),
//        header: Text('This is the header!'),
//        onReorder: (oldIndex, newIndex) {
//          setState(() {
//            _updateMyItems(oldIndex, newIndex);
//          });
//        },
//      ),
//    );
//  }

  void _updateMyItems(int oldIndex, int newIndex) {
    var tmp = list[oldIndex];
    list[oldIndex] = list[newIndex];
    list[newIndex] = tmp;
  }

  Widget f(int i) {
    return ListTile(
      key: ValueKey("$i"),
      title: Text('Item $i'),
    );
  }
}
